package com.pl.config.security;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.util.UrlUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.pl.bean.ManageUserBean;
import com.pl.bean.ResultData;
import com.pl.service.ManageUserService;
import com.pl.util.ResultDataUtil;

/**
 * class name:MyAuthenctiationSuccessHandler <BR>
 * class description: 登录成功的自定义处理 <BR>
 * Remark: <BR>
 * @version 1.00 2019年11月22日
 * @author wangjian
 */
@Component
public class MyAuthenctiationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
	
	@Resource(name="manageUserService")
	private ManageUserService manageUserService;
	
	private RequestCache requestCache = new HttpSessionRequestCache();
	
	//认证服务器端口
	@Value("${oauth2-login-server.server-port}")
    private int serverPort;
	
	//认证服务器地址
    @Value("${oauth2-login-server.server-name}")
    private String serverName;
	
	private static Logger log = LogManager.getLogger();
 
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
    	
    	ManageUserBean userBean = manageUserService.getManageUser(authentication);
    	manageUserService.afterSuccessLogin(request, userBean);
    	
    	Map<String, String> dataMap = new HashMap<String, String>();
    	dataMap.put("type", "selectClient");
    	
    	DefaultSavedRequest savedRequest = (DefaultSavedRequest) requestCache.getRequest(request, response);
    	if(savedRequest !=null) {
    		//有重定向地址的登录
    		String targetUrl = UrlUtils.buildFullRequestUrl(savedRequest.getScheme(), serverName, serverPort, savedRequest.getRequestURI(), savedRequest.getQueryString());
    		
    		dataMap.put("type", "redirect");
			dataMap.put("targetUrl", targetUrl);
    	}
    	
    	log.info(" onAuthenticationSuccess sessionId={}", request.getSession().getId());
    	
    	ResultData<Map<String, String>> result = ResultDataUtil.ok(dataMap);
    	
        response.setStatus(200);
        response.setContentType("application/json;charset=UTF-8");
        
        PrintWriter out = response.getWriter();
        
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }
}
